//
//  MJRefreshAutoNormalFooter.swift
//  OC-Swift
//
//  Created by 陆文宇 on 2017/2/15.
//  Copyright © 2017年 陆文宇. All rights reserved.
//

import UIKit

class MJRefreshAutoNormalFooter: MJRefreshAutoStateFooter {
    
    var activityIndicatorViewStyle: UIActivityIndicatorViewStyle = .gray {
        didSet {
            loadingView.activityIndicatorViewStyle = activityIndicatorViewStyle
            loadingView.removeFromSuperview()
            addSubview(loadingView)
            setNeedsLayout()
        }
    }
    lazy var loadingView: UIActivityIndicatorView = {
        let _loadingView: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .gray)
        _loadingView.hidesWhenStopped = true
        return _loadingView
    }()
    override var state: MJRefreshComponent.MJRefreshState {
        set {
            if super.state != newValue {
                super.state = newValue
            }
            
            if state == .noMoreData || state == .idle {
                loadingView.stopAnimating()
            } else if state == .refreshing {
                loadingView.startAnimating()
            }
        }
        
        get {
            return _state
        }
    }
    override init(refreshingClosure: @escaping MJRefreshComponentRefreshingClosure) {
        super.init(refreshingClosure: refreshingClosure)
        
        addSubview(loadingView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func prepare() {
        super.prepare()
    }
    
    override func placeSubviews() {
        super.placeSubviews()
        
        if loadingView.constraints.count != 0 {
            return
        }
        
        var loadingCenterX: CGFloat = mj_w * 0.5
        if !isRefreshingTitleHidden {
            loadingCenterX -= stateLabel.mj_textWith * 0.5 + labelLeftInset
        }
        let loadingCenterY: CGFloat = mj_h * 0.5
        loadingView.center = CGPoint(x: loadingCenterX, y: loadingCenterY)
    }


    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
